/*
 *   JVending
 *   Copyright (C) 2005  Shane Isbell
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.jvending.provisioning.adapter;

import javax.provisioning.*;
import javax.provisioning.adapter.Adapter;
import javax.provisioning.adapter.AdapterException;
import java.net.URL;

/**
 * @author Shane Isbell
 * @since 1.0
 */

public final class JnlpAdapter extends Adapter {

    /**
     * Default Constructor. This class is meant to be instantiated by the framework, not by the application developer.
     */
    public JnlpAdapter() {
    }

    public DescriptorFile createDescriptorFile(URL url) throws AdapterException {
        throw new AdapterException("JNLP", "Not currently supported");
    }

    public String createFulfillmentURI(FulfillmentTask fulfillmentTask) throws AdapterException {
        if (fulfillmentTask == null) throw new AdapterException("JNLP", "JV-1201-001: FulfillmentTask is null");

        try {
            return fulfillmentTask.getDeliveryURI();//Spec: 7.3.1 Check - is this relative to the adapters URI namespace?
        } catch (ProvisioningException e) {
            DeliveryEvent deliveryEvent = new DeliveryEvent(fulfillmentTask.getDeliveryContext(),
                    fulfillmentTask.getAdapterName(), 0, fulfillmentTask.getFulfillmentID(), 0,
                    "JV-1201-002: Could not get the delivery URI: Message = " + e.getMessage());

            DeliveryComponent deliveryComponent = adapterConfig.getAdapterContext().getDeliveryComponent();

            try {
                deliveryComponent.handleDeliveryEvent(deliveryEvent);
            } catch (ProvisioningException e1) {
                throw new AdapterException(fulfillmentTask.getAdapterName(),
                        "JV-1201-003:Could not find the Delivery URI for this task and was unable to inform the delivery component of this event");
            }

            throw new AdapterException(fulfillmentTask.getAdapterName(), "JV-1201-004: Could not find the Delivery URI for this task");
        }
    }
}